package com.zxc.gulimall.order.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zxc.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118653116";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQC8z2gv51TNBDG0XPJEvVXzK/eGshvudWeMIudZAvP/jEnSmu0kXIzY7ZimkQXeUd/cqeIPQRuOwRUp0l/xiISnBFIu5qOi7I9lFWoq9oWtsRYnx8/Af7pUjarUdP1jSl6wl2Hmf63z38XgFTGyREUiCWL6AtHhEd+tdpS3XIjhuAw5nnCoClLgMsOO1lTj+XNLS5s2f6GEZL5hK4a7Mn3oPSN+hg0JqTREImzG2LsFxijgEIZlKC7gkg7uWAq3VoGML1fcZa1bY8RvrrhquA8firL87n8tPPll4BOPkPbJUoDmgB6ujhg2neWgcpysi/Y6gzKq8k8ky1LUpaYJLPaNAgMBAAECggEBAIR4TJQXjRLOl4UBFWWPJaeYOHHDsv8qVj0nHAQCo2ivZ/gghLNXT+a/RLPQ2joyXYs3kgi72HbfF1chZ/lRnCiGSpbncPXVXYJBdRyxm/bSYUYF1cgslXHPd4TalBE+05K4k5KiEE0junROynBVn9+SkGPy6Igt/y54HT1M6ijawNdPiMZm9IOUyrWJWbIXG3yUy7YsU4vmb0CV33UbQgisNAkW2zhGo8zgwxTjKmskaobF2ddXgPdx/fHiMhe8mO+hX5aXFuI4ibD9lU4BO3f5klxjm9tFP58o9otdrTcVw3KtBfjcDiybyxC4wbK2JqPM7uGN1u2/rIkn+N9KYuECgYEA5FaJzo2B5Cr9EP9MJwop0BZ5z2DNUYtyi5V9sO3unDedwNI7cE69xBxH0K0G5FrFHuRnUQP7zFDbSOmw7kg7MlWBwejX4PdggLwG/KBM0ReHwTSt1H5Acj5hnMy23zEzSrRIaLLJyUNM0iwS56AW1f5RZ360Kyi+KDWgknDCwXkCgYEA0679I0gSN8O9QPhrmduzFvN/9Smga4IUZ4aExkTAUJhiyN5k5nt5GPtwUKEqJher+YkaBQqUchybFBkEmde7sdiw62KYIQsE6yLnX8ubfN7mllSWbWnncy5qryWpuVIfJFVHeuP3Lbmy2sNlBZjN6L50WuawdhXhoH31A5wMjLUCgYEAghfX/gnbln7zniqGXbvJpNgZYJDbifk0MtjiOtrk1rZNN93PpaYHMmOWewsJ4TLiGj6xqpPlczwiI8GZX7lhmMTk3/DkhjULE22NYYOHIJ4SZ363B1lDKJPoE2kDS9iBS6+5jYVHuFaSvzADOVqZiCSUhq3Gwsw8wzYCnUyHeakCgYEAvCtUaFV7pEiCZkut0rIii6deun8LVCXon3JLerfIwJwFTAutY9ikeBmi6HmrLvth60r9bzfNZ3aYQBBRL7Q+68P02PjmxJC4CZEYXeZvTFwUW9PLeG1oydGcEh+HtO4UMsb/6HTW17W4EJEarxP7DganBdsFR6MB0xnEgDXqTfUCgYEAyfqW37lyyTAbC4dU3XZuxn9JItgoML/SBsngjfC7YattVMau65Mjg6knJQ06YtDZb5ziwFRdF5biiSUgVOVlII79/gFiYAXOBvhLojjpFgig/O5GI1v5hK9P1wFIU2DsjnoOUkES/kAT7E/LuAlffYbMPjl1FmLGrz+OSrAZKDE=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlhBa0TNzJDIPABRcB+ISdf2O9j6H8tpccuoIo4vNDWfWKAJsbM4cGKRpe0T8lHe85HPYDTzf2M6/O/4y12fdS1mqHvBFAMvcd/h2Did64OOeojcv5NqIa+CLFp/5AOlF8MpqJ0ftbCp8Zy9iUP1zxzRvPvRHYNbRYRSRX7JqLhnlPfnFZbgtKVgtsFuvF+eJDymyIfhkwED45NtGy2sGNb71nWq0zhrzR1i9Hs8hVifJ+7Burl0UBbOufNtzNafknrxYU0PcY/SGipRdhPsB0QGqohT2dXDN7Ber8l1VF+jTgvCaDoYVW4UwYF5TmXx0nJ2M9aJX9VAu29HyoclU4QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://fdyjcahkbm.51xd.pub/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        //timout ==》timeout分钟订单不支付我们关闭订单，为了防止我们的用户长时间不支付，到达我们订单过期时间，导致库存解锁，订单为支付状态问题
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
